import axio from "../assets/axio.js";

const imgType = ["jpg", "png", "jiff", "jpeg", "bmp", "gif"];
const videoType = ["mp4", "flv", "jpmg"];
const fileImageTypes = imgType.concat(videoType);

export const view_download = {
    template: `
    <div id="view_download">
      <table class="table_file_list">
          <tr class="table_header">
              <th class="list_name">文件名</th>
              <th class="list_size">大小</th>
              <th class="list_action">操作</th>
          </tr>
          <tr v-for="fileitems in fileList" :key="fileitems">
              <td class="list_name">
                  <span :style="'padding-left:' + fileitems.layer + 'em'">
                      <!-- 文件层次树占位 -->
                  </span>
                  <span class="filename" :class="processDownloadable(fileitems.type)?'downloadable':''" @click="handleDownload(fileitems.url,fileitems.filename,fileitems.type)" :download="fileitems.filename">
                      <span v-html="addAIcon(fileitems.type)"></span>
                      {{ fileitems.filename }}
                  </span>
              </td>
              <td class="list_size">
              	  {{ fileitems.size }}
              </td>
              <td class="list_action">
              	  <a class="action_item view" v-if="isFile(fileitems.url)" :href="fileitems.url" target="_blank">[查看]</a>
              	  <span class="action_item delete"
                      @click="handleDeleteFile(fileitems.filename, fileitems.url)">{{fileitems.type=='dir'?'':'[删除]'}}</span>
              </td>
          </tr>
      </table>
      <p style="margin: 0">&ensp;{{ msg }}</p>
    </div>
    <div id="div_Rename" v-if="displayRenameAlertWindow">
      您确定要重命名吗？
      <input type="text" name="" id="" v-model="tempNewName" />
      <p>*不支持修改拓展名*</p>
      <button @click="handleRenameFile(tempNewName, tempURL)">确定</button>
      &emsp;
      <button @click="displayRenameAlertWindow = false">取消</button>
    </div>
      `,
    name: "view_download",
    data() {
        return {
            displayRenameAlertWindow: false,
            tempNewName: "none",
            tempURL: "",
            msg: "完毕",
            //fileList: [],
            fileList: this.getFileList(),
        };
    },
    computed: {
        isFile() {
            return (param_url) => {
                if (param_url) {
                    for (let i = 0; i < fileImageTypes.length; i++) {
                        if (param_url.indexOf(fileImageTypes[i]) != -1) {
                            return true;
                        }
                    }
                }
                return false;
            };
        }, processDownloadable() {
            return (param_filetype) => {
                console.log(param_filetype);
                if (param_filetype == 'dir') {
                    return false
                } else {
                    return true
                }
            }
        }
    },
    methods: {
        addAIcon(parm) {
            if (parm == "dir") {
                return '<i class="el-icon-folder-opened"></i>';
            } else {
                return "";
            }
        },
        handleDeleteFile(filename, fileUrl) {
            let flag = confirm("您确定要删除" + filename + "吗？");
            if (flag) {
                axio
                    .post("/fields/delete", fileUrl)
                    .then((response) => {
                        alert(response);
                        this.getFileList();
                    });
            }
        },
        getFileList() {
            axio
                .get("/fields/tree")
                .then((response) => {
                    //console.log(response);
                    this.fileList = JSON.parse(response);
                })
                .catch((error) => {
                    console.log(error);
                    if (error.toString().indexOf("401") != -1) {
                        alert("检测到未登陆，立即跳转到登录页面");
                    }
                });
            return [
                {
                    "type": "dir",
                    "filename": "download",
                    "layer": 0
                },
                {
                    "type": "fileManager",
                    "layer": 1,
                    "filename": "103785554_p0a.jpg",
                    "url": "/fields/download/103785554_p0a.jpg",
                    "size": "388.9KB"
                },
                {
                    "type": "fileManager",
                    "layer": 1,
                    "filename": "10772200_p0.jpg",
                    "url": "/fields/download/10772200_p0.jpg",
                    "size": "83.9KB"
                },
                {
                    "type": "fileManager",
                    "layer": 1,
                    "filename": "210143_01.png",
                    "url": "/fields/download/210143_01.png",
                    "size": "34.3KB"
                },
                {
                    "type": "fileManager",
                    "layer": 1,
                    "filename": "400009_01.png",
                    "url": "/fields/download/400009_01.png",
                    "size": "33.5KB"
                },
                {
                    "type": "fileManager",
                    "layer": 1,
                    "filename": "56879149.jpg",
                    "url": "/fields/download/56879149.jpg",
                    "size": "156.5KB"
                },
                {
                    "type": "fileManager",
                    "layer": 1,
                    "filename": "56879149.png",
                    "url": "/fields/download/56879149.png",
                    "size": "218.1KB"
                },
                {
                    "type": "fileManager",
                    "layer": 1,
                    "filename": "56879149avatar.jpg",
                    "url": "/fields/download/56879149avatar.jpg",
                    "size": "151.0KB"
                }
            ]
        },
        handleDownload(url, filename, filetype = 'dir') {
            if (filetype != 'dir') {
                axio.stream("get", url, filename);
            }
        },
    },
    mounted() {
    },
};

